trigger Assignmetn6_2 on Class__c (before delete) {
    //TODO: how to handle if we retrieve more than 1000 students here ???   
    List<Student__c> femateStudentList = new List<Student__c>([Select Class__r.id from Student__c where Sex__c = 'Female' ]);
    Set<id> clsIds = new Set<id>(); 
    //prepare a set of class ids 
    for( Student__c student : femateStudentList ){
        clsIds.add(student.Class__r.id);
    }
    for(Class__c cls : Trigger.old) {
    
        /** basic method discarded*/
        /*Integer femaleCount = [Select count() from Student__c where Sex__c = 'Female' and Class__r.id =: cls.id];
        if(femaleCount > 1){
            cls.addError('Class has more than one female students. Can not delete.');
        }*/  
        
        if(clsIds.contains(cls.id)){
            cls.addError('Class has more than one female students. Can not delete.');
        }
            
    }
    
    

}